{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 最新版本\n",
    "\n",
    "![Asked by Amazon](https://img.shields.io/badge/Asked%20by-Amazon-important?style=social&logo=amazon)\n",
    "\n",
    "&emsp;&emsp;版本号是用于标识软件产品的唯一状态字符串。版本号的格式为：`a.b.c.d`，其中的所有变量是由小数点分隔的数字字符串，它们从左到右分别表示重要到次要层次的变更。给定两个版本号`version1`和`version2`，确定哪个是新版本。\n",
    "\n",
    "> 若`version1 > version2`，返回1；\n",
    "> 若`version1 < version2`，返回-1；\n",
    "> 否则，返回0。\n",
    "\n",
    "```text\n",
    " In[1]: version1 = \"1.0.33\"\n",
    "        version2 = \"1.0.27\"\n",
    "Out[1]: 1   # ver1 > ver2\n",
    "\n",
    " In[2]: version1 = \"0.1\"\n",
    "        version2 = \"1.1\"\n",
    "Out[2]: -1  # ver1 < ver2\n",
    "\n",
    " In[3]: version1 = \"1.01\"\n",
    "        version2 = \"1.001\"\n",
    "Out[3]: 0   # 去除前导0，\"01\"与\"001\"相同\n",
    "\n",
    " In[4]: version1 = \"1.0\"\n",
    "        version2 = \"1.0.0\"\n",
    "Out[4]: 0   # 版本1省略的第三级修订号默认为0\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        version1 = [int(item) for item in version1.split(\".\")]\n",
    "        version2 = [int(item) for item in version2.split(\".\")]\n",
    "        for i in range(max(len(version1), len(version2))):\n",
    "            a = version1[i] if i < len(version1) else 0\n",
    "            b = version2[i] if i < len(version2) else 0\n",
    "            if a > b:\n",
    "                return 1\n",
    "            elif a < b:\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "-1\n",
      "0\n",
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "solution = Solution()\n",
    "print(solution.compareVersion(\"1.0.33\", \"1.0.27\"))\n",
    "print(solution.compareVersion(\"0.1\", \"1.1\"))\n",
    "print(solution.compareVersion(\"1.01\", \"1.001\"))\n",
    "print(solution.compareVersion(\"1.0\", \"1.0.0\"))\n",
    "print(solution.compareVersion(\"1.0.1\", \"1\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
